package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.dzbean.req.ModifyPwdRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.SetPayPwdRequest;
import sz.szsmk.citizencard.mvp.PayPwdSetOrModifyMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

/**
 * Created by chensf on 17/10/1.
 */

public class PayPwdSetOrModifyPresenter extends AppBasePresenter<PayPwdSetOrModifyMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;

    @Inject
    public PayPwdSetOrModifyPresenter() {
        getComponent().inject(this);
    }

    public void setPayPassword(String pwd) {
        checkViewAttached();

        SetPayPwdRequest setPayPassword = new SetPayPwdRequest();
        setPayPassword.setPassword(pwd);
        ObjectSignatureUtil.signatureRequest(setPayPassword);

        mNetworkDateSource.setPayPassword(setPayPassword, new Action1<GetCodeResponse>() {
            @Override
            public void call(GetCodeResponse getCodeResponse) {
                if (getCodeResponse.getCode().equals("0000")) {
                    getMvpView().onPayPwdSetSuccess();
                } else {
                    getMvpView().onPayPwdSetFailure();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void modifyPayPassword(String oldPassword, String newPassword) {
        checkViewAttached();
        ModifyPwdRequest modifyPwdRequest = new ModifyPwdRequest();
        modifyPwdRequest.setNewPassword(newPassword);
        modifyPwdRequest.setOldPassword(oldPassword);
        ObjectSignatureUtil.signatureRequest(modifyPwdRequest);
        mNetworkDateSource.updatePayPassword(modifyPwdRequest, new Action1<GetCodeResponse>() {
            @Override
            public void call(GetCodeResponse getCodeResponse) {
                if (getCodeResponse.getCode().equals("0000")) {
                    getMvpView().onPayPwdModifySuccess();
                } else {
                    getMvpView().onPayPwdModifyFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

}
